package com.cliff.cloudnote.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cliff.cloudnote.bean.UserBean;
import com.cliff.cloudnote.dao.UserDao;
import com.cliff.cloudnote.model.User;
import com.cliff.util.MD5Util;

@Service
public class UserServiceImpl implements UserService {
	private static final String MD5KEY = "WWW.CLIFFMIGNON.COM";
	@Autowired
	private UserDao userDao;
	
	public boolean checkPassword(UserBean bean) {
		User user = userDao.findOneByUsername(bean.getUsername());
		String encryptPwd = MD5Util.encrypt(bean.getPassword() + MD5KEY);
		if (encryptPwd.equals(user.getPassword())) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean checkExistUser(UserBean bean) {
		User user = userDao.findOneByUsername(bean.getUsername());
		if (user != null) {
			return true;
		} else {
			return false;
		}
	}
	
	public void registerUser(UserBean bean) {
		User user = new User();
		user.setUsername(bean.getUsername());
		user.setPassword(MD5Util.encrypt(bean.getPassword() + MD5KEY));
		user.setEmail(bean.getEmail());
		userDao.store(user);
	}
}
